package com.pfs.internal.timestamp.actions;

import java.util.List;
import java.util.Map;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.pfs.internal.timestamp.entities.HWIndependentDevices;
import com.pfs.internal.timestamp.repositories.HWIndependentDevicesRepository;

@SuppressWarnings("serial")
public class DevicesAction extends ActionSupport {
	private List<HWIndependentDevices> devices;
	Map<String, Object> session = ActionContext.getContext().getSession();

	@Action(value = "ts-devices-all-list", results = { @Result(name = "success", type = "json") })
	public String listDevices() {
		WebApplicationContext context = WebApplicationContextUtils.getRequiredWebApplicationContext(ServletActionContext.getServletContext());
		HWIndependentDevicesRepository devicesRepository = (HWIndependentDevicesRepository) context.getBean("hWIndependentDevicesRepository");
		devices = devicesRepository.findAll();
		//System.out.println(devices.size());
		//HWIndependentDevices device = devicesRepository.findOne(26);
		System.out.println();
		return SUCCESS;
	}
	
	@Action(value = "ts-devices-in-list", results = { @Result(name = "success", type = "json") })
	public String listDevicesIn() {
		WebApplicationContext context = WebApplicationContextUtils.getRequiredWebApplicationContext(ServletActionContext.getServletContext());
		HWIndependentDevicesRepository devicesRepository = (HWIndependentDevicesRepository) context.getBean("hWIndependentDevicesRepository");
		devices = devicesRepository.findAllIn();
		System.out.println(devices.size());
		return SUCCESS;
	}
	
	public List<HWIndependentDevices> getDevices() {
		return devices;
	}

	public void setDevices(List<HWIndependentDevices> devices) {
		this.devices = devices;
	}

	public Map<String, Object> getSession() {
		return session;
	}

	public void setSession(Map<String, Object> session) {
		this.session = session;
	}

}
